import * as $api from '@/api/Api.js'
import $http from "@/utils/request.js";
export default{
    getversion(){
		//#ifdef APP-PLUS
		let bCode
		let downUrl
		let tiMsg
		uni.getSystemInfo({
			success(res) {
				let opts = $api.Version
				$http.post(opts).then((res) => {
					
					bCode = res.data.code
					downUrl = res.data.down
					tiMsg = res.msg
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						if (bCode > parseInt(wgtinfo.versionCode)) {
							uni.showModal({ //提醒用户更新
								title: "更新提示",
								content: tiMsg,
								showCancel: false,
								success: (res) => {
									if (res.confirm) {
										// var showLoading = plus.nativeUI.showWaiting("正在下载");
										var downloadTask = uni.downloadFile({
											url: downUrl,
											success: (downloadResult) => {
												console.log(downloadResult)
													if (downloadResult.statusCode ===200) {
														plus.runtime.install(downloadResult.tempFilePath, {force: true},
																function() {
																	plus.cache.clear();
																	showLoading.close();
																	plus.runtime.restart();
																},
																function(e) {
																	showLoading.close();
																	console.error('install fail...');
																});
													}
												}
										});
										var showLoading = plus.nativeUI
											.showWaiting("正在下载");
										downloadTask.onProgressUpdate((res) => {
											showLoading.setTitle("正在下载" + res.progress + "%");
										});
									}
								}
							})
						}
					})
				})
			}
		});
		//#endif  
	}
}

